package me.firstapp.service.bbs.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.bbs.Section;
import me.firstapp.repository.bbs.ReplyRepository;
import me.firstapp.repository.bbs.SectionRepository;
import me.firstapp.repository.bbs.TopicRepository;
import me.firstapp.service.bbs.SectionService;

@Service
public class SectionServiceImpl implements SectionService {

	@Autowired
	private SectionRepository sectionRepository;

	@Autowired
	private TopicRepository topicRepository;

	@Autowired
	private ReplyRepository replyRepository;

	@Override
	public List<Section> findAll(Long apiClientId) {
		return sectionRepository.findSections(apiClientId);
	}

	@Override
	public Section addSection(String name, String description, Integer sort, Long apiClientId) {
		if (StrUtils.isNULL(name)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "板块名称");
		}
		if (sort == null) {
			sort = 0;
		}
		Section section = new Section();
		section.setName(name);
		section.setDescription(description);
		section.setSort(sort);
		section.setTopicCounts(0L);
		section.setReplyCounts(0L);
		section.setApiClientId(apiClientId);
		sectionRepository.save(section);
		return section;
	}

	@Override
	public Page<Section> findSectionPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType, Long apiClientId) {
		return sectionRepository.findSectionPage(pageNo, pageSize, searchParams, sortParam, sortType, apiClientId);
	}

	@Override
	public void deleteSections(String ids, Long apiClientId) {
		if (StrUtils.isNULL(ids)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "板块id");
		}
		if (!ids.contains(",")) {
			Section section = sectionRepository.findSectionById(Long.valueOf(ids), apiClientId);
			if (section != null) {
				sectionRepository.deleteById(Long.valueOf(ids));
			}
		} else {
			String[] _ids = ids.split(",");
			Long[] _ids_ = new Long[_ids.length];
			for (int i = 0; i < _ids.length; i++) {
				Section section = sectionRepository.findSectionById(Long.valueOf(_ids[i]), apiClientId);
				if (section != null) {
					_ids_[i] = Long.valueOf(_ids[i]);
				}
			}
			if (_ids.length > 0) {
				sectionRepository.delete(_ids_);
			}
		}
	}

	@Override
	public Section updateSection(Long id, String name, String description, Integer sort, Long topicCounts,
			Long replyCounts, Long apiClientId) {
		if (id == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "板块id");
		}
		Section section = (Section) sectionRepository.findSectionById(id, apiClientId);
		if (section == null) {
			throw new ServiceException(StatusHouse.SECTION_NOT_FOUND);
		}
		if (StrUtils.isNotNULL(name)) {
			section.setName(name);
		}
		if (StrUtils.isNotNULL(description)) {
			section.setDescription(description);
		}
		if (sort != null) {
			section.setSort(sort);
		}
		if (topicCounts != null) {
			section.setTopicCounts(topicCounts);
		}
		if (replyCounts != null) {
			section.setReplyCounts(replyCounts);
		}
		sectionRepository.update(section);
		return section;
	}

	@Override
	public Section findById(Long id, Long apiClientId) {
		return sectionRepository.findSectionById(id, apiClientId);
	}

	@Override
	public void updateTopicAndReplyCounts(Long apiClientId) {
		List<Section> sections = sectionRepository.findSections(apiClientId);
		for (Section section : sections) {
			Long sTopicCounts = topicRepository.findTopicsCountsBySectionId(section.getId(), apiClientId);
			Long sReplyCounts = replyRepository.findReplyCountsBySectionId(section.getId(), apiClientId);
			if (sTopicCounts != null) {
				section.setTopicCounts(sTopicCounts);
			}
			if (sReplyCounts != null) {
				section.setReplyCounts(sReplyCounts);
			}
			sectionRepository.update(section);
		}
	}

}
